package com.example.mediaandroidjava;

import androidx.appcompat.app.AppCompatActivity;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.Player;
import androidx.media3.common.TrackSelectionOverride;
import androidx.media3.common.Tracks;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector;

import android.os.Bundle;

import com.google.common.collect.ImmutableList;

public class TrackSelectionActivity2 extends AppCompatActivity {
    ExoPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_track_selection2);
        player.addListener(new Player.Listener() {
            @Override
            public void onTracksChanged(Tracks tracks) {
                ///曲目更改
                Player.Listener.super.onTracksChanged(tracks);
                //查询可用曲目
                for (Tracks.Group trackGroup : tracks.getGroups()) {
                    // Group level information.
                    @C.TrackType int trackType = trackGroup.getType();
                    boolean trackInGroupIsSelected = trackGroup.isSelected();
                    boolean trackInGroupIsSupported = trackGroup.isSupported();
                    for (int i = 0; i < trackGroup.length; i++) {
                        // Individual track information.
                        boolean isSupported = trackGroup.isTrackSupported(i);
                        boolean isSelected = trackGroup.isTrackSelected(i);
                        Format trackFormat = trackGroup.getTrackFormat(i);
                    }
                }
            }
        });
    }

    void trackSelection() {
        player.setTrackSelectionParameters(
                player
                        .getTrackSelectionParameters()
                        .buildUpon()
                        .setMaxVideoSizeSd()
                        .setPreferredAudioLanguage("hu")
                        .build());
    }

    void setOverrideForType(Tracks.Group audioTrackGroup) {
        player.setTrackSelectionParameters(
                player
                        .getTrackSelectionParameters()
                        .buildUpon()
                        .setOverrideForType(
                                new TrackSelectionOverride(
                                        audioTrackGroup.getMediaTrackGroup(), /* trackIndex= */ 0))
                        .build());
    }

    void setTrackTypeDisabled() {
        player.setTrackSelectionParameters(
                player
                        .getTrackSelectionParameters()
                        .buildUpon()
                        .setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, /* disabled= */ true)
                        .build());
    }


    void addOverride(Tracks.Group disabledTrackGroup) {
        player.setTrackSelectionParameters(
                player
                        .getTrackSelectionParameters()
                        .buildUpon()
                        .addOverride(
                                new TrackSelectionOverride(
                                        disabledTrackGroup.getMediaTrackGroup(),
                                        /* trackIndices= */ ImmutableList.of()))
                        .build());
    }

    void setTrackSelector() {
        DefaultTrackSelector trackSelector = new DefaultTrackSelector(this);
        ExoPlayer player = new ExoPlayer.Builder(this).setTrackSelector(trackSelector).build();
        trackSelector.setParameters(
                trackSelector.buildUponParameters().setAllowVideoMixedMimeTypeAdaptiveness(true));
    }
}